package com.cyf.smartCity.service.impl;

import com.cyf.db.bean.*;
import com.cyf.db.mapper.smartCity.*;
import com.cyf.smartCity.bean.*;
import com.cyf.smartCity.service.StatisticsService;
import com.cyf.util.DateUtil;
import com.cyf.util.DoubleUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private LocalSentryBoxMapper localSentryBoxMapper;
    @Autowired
    private LocalCarFlowMapper localCarFlowMapper;
    @Autowired
    private LocalFeeFlowMapper localFeeFlowMapper;
    @Autowired
    private LocalFeeGroupMapper localFeeGroupMapper;
    @Autowired
    private LocalPayWayFlowMapper localPayWayFlowMapper;
    @Autowired
    private LocalSettleFlowMapper localSettleFlowMapper;
    @Autowired
    private LocalCarTimeFlowMapper localCarTimeFlowMapper;
    @Autowired
    private LocalIctMapper localIctMapper;


    @Override
    public WechatParkListSentryBoxRsp listSentryBox(Integer parkingId) {
        WechatParkListSentryBoxRsp rsp = new WechatParkListSentryBoxRsp();
        List<LocalSentryBox> sentryBoxList = localSentryBoxMapper.listNameInfoByParkingId(parkingId);
        if (CollectionUtils.isNotEmpty(sentryBoxList)) {
            List<WechatParkListSentryBoxRsp.Info> list = new ArrayList<>();
            for (LocalSentryBox sentryBox : sentryBoxList) {
                WechatParkListSentryBoxRsp.Info item = new WechatParkListSentryBoxRsp.Info();
                item.setLocalSentryboxId(sentryBox.getLocalSentryboxId());
                item.setName(sentryBox.getName());
                list.add(item);
            }
            rsp.setInfoList(list);
        }
        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkReportCarFlowRsp reportCarFlow(WechatParkReportCarFlowReq req) {
        WechatParkReportCarFlowRsp rsp = new WechatParkReportCarFlowRsp();

        String searchDate = req.getSearchDate().replaceAll("-", "");
        Integer[] inCarFlowArr = new Integer[24];
        for (int i = 0; i < 24; i++) {
            inCarFlowArr[i] = 0;
        }
        List<LocalCarFlow> inCarFlowList = localCarFlowMapper.listHourCarFlowByCondition(req.getParkingId(),
                searchDate, LocalCarFlow.DEVICE_TYPE_IN, req.getLocalSentryboxId());
        if (CollectionUtils.isNotEmpty(inCarFlowList)) {
            for (LocalCarFlow carFlow : inCarFlowList) {
                int timeKey = carFlow.getTime_key();
                inCarFlowArr[timeKey % 100] = carFlow.getCarNum();
            }
        }
        rsp.setInCarNum(Arrays.asList(inCarFlowArr));

        Integer[] outCarFlowArr = new Integer[24];
        for (int i = 0; i < 24; i++) {
            outCarFlowArr[i] = 0;
        }
        List<LocalCarFlow> outCarFlowList = localCarFlowMapper.listHourCarFlowByCondition(req.getParkingId(),
                searchDate, LocalCarFlow.DEVICE_TYPE_OUT, req.getLocalSentryboxId());
        if (CollectionUtils.isNotEmpty(outCarFlowList)) {
            for (LocalCarFlow carFlow : outCarFlowList) {
                int timeKey = carFlow.getTime_key();
                outCarFlowArr[timeKey % 100] = carFlow.getCarNum();
            }
        }
        rsp.setOutCarNum(Arrays.asList(outCarFlowArr));

        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkReportFeeFlowRsp reportFeeFlow(WechatParkReportReq req) {
        WechatParkReportFeeFlowRsp rsp = new WechatParkReportFeeFlowRsp();

        String dateStr = req.getSearchDate().replaceAll("-", "");
        List<LocalFeeFlow> localFeeFlowList = localFeeFlowMapper.sumActualFeeAndWechatFeeByTimeKey(Integer.valueOf(dateStr), req.getParkingId());
        if (CollectionUtils.isNotEmpty(localFeeFlowList)) {
            List<WechatParkReportFeeFlowRsp.Info> list = new ArrayList<>();
            for (LocalFeeFlow localFeeFlow : localFeeFlowList) {
                WechatParkReportFeeFlowRsp.Info item = new WechatParkReportFeeFlowRsp.Info();
                String feeGroupName = localFeeGroupMapper.getNameByParkingIdAndLocalId(req.getParkingId(), localFeeFlow.getFeeGroupId());
                Integer paid = localFeeFlow.getPaid();
                if (paid % 100 == 0) {
                    item.setName(feeGroupName + " " + paid / 100 + "元");
                    item.setValue(paid / 100);
                } else {
                    double fee = Double.parseDouble(DoubleUtil.format(paid * 0.01));
                    item.setName(feeGroupName + " " + paid / 100 + "元");
                    item.setValue(fee);
                }

                list.add(item);
            }
            rsp.setFeeGroupWayList(list);
        }

        List<LocalPayWayFlow> localPayWayFlowList = localPayWayFlowMapper.listByTimeKey(Integer.valueOf(dateStr), req.getParkingId());
        if (CollectionUtils.isNotEmpty(localPayWayFlowList)) {
            List<WechatParkReportFeeFlowRsp.Info> list = new ArrayList<>();
            for (LocalPayWayFlow payWayFlow : localPayWayFlowList) {
                Integer paid = payWayFlow.getActualFee();
                WechatParkReportFeeFlowRsp.Info item = new WechatParkReportFeeFlowRsp.Info();
                if (paid % 100 == 0) {
                    item.setName(payWayFlow.getPayWayDesc() + " " + paid / 100 + "元");
                    item.setValue(paid / 100);
                } else {
                    double fee = Double.parseDouble(DoubleUtil.format(paid * 0.01));
                    item.setName(payWayFlow.getPayWayDesc() + " " + fee + "元");
                    item.setValue(fee);
                }
                list.add(item);
            }
            //纸币收款
            LocalIct localIct = localIctMapper.selectIctByTime(req);
            if(localIct!= null){
                WechatParkReportFeeFlowRsp.Info item1 = new WechatParkReportFeeFlowRsp.Info();
                item1.setName("纸币机收款");
                item1.setValue(localIct.getActualFee()/100);
                list.add(item1);
                WechatParkReportFeeFlowRsp.Info item2 = new WechatParkReportFeeFlowRsp.Info();
                item2.setName("纸币机找零");
                item2.setValue(localIct.getChangeAmount()/100);
                list.add(item2);
            }
            rsp.setPayWayList(list);
        }
        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkReportSettleFlowRsp reportSettleFlow(WechatParkReportReq req) {
        WechatParkReportSettleFlowRsp rsp = new WechatParkReportSettleFlowRsp();
        String dateStr = req.getSearchDate().replaceAll("-", "");
        List<LocalSettleFlow> settleFlowList = localSettleFlowMapper.listByTimeKey(Integer.parseInt(dateStr), req.getParkingId());
        if (CollectionUtils.isNotEmpty(settleFlowList)) {
            List<WechatParkReportSettleFlowRsp.Info> list = new ArrayList<>();
            for (LocalSettleFlow settleFlow : settleFlowList) {
                WechatParkReportSettleFlowRsp.Info item = new WechatParkReportSettleFlowRsp.Info();
                item.setId(settleFlow.getId());
                item.setName(settleFlow.getName());
                item.setCount(settleFlow.getCount());
                item.setActualFee(settleFlow.getActualFee());
                list.add(item);
            }
            rsp.setInfoList(list);
        }
        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }

    @Override
    public WechatParkReportCarTimeFlowRsp reportCarTimeFlow(WechatParkReportCarTimeFlowReq req) {
        WechatParkReportCarTimeFlowRsp rsp = new WechatParkReportCarTimeFlowRsp();

        Date date;
        try {
            date = DateUtil.convertStr2Date(req.getEndTime(), DateUtil.FORMAT_YYYY_MM_DD);
        } catch (ParseException e) {
            rsp.setResult(WechatBasicRsp.FAIL);
            rsp.setStrError("请稍后重试！");
            return rsp;
        }

        List<String> dateList = new ArrayList<>();
        List<Integer> expireCountList = new ArrayList<>();
        List<Integer> delayCountList = new ArrayList<>();

        for (int i = 6; i > 0; i--) {
            Date date1 = DateUtil.getDateAfterSeconds(date, i * (-24 * 3600));
            String dateStr = DateUtil.convertDate2Str(date1, DateUtil.FORMAT_YYYYMMDD);
            LocalCarTimeFlow carTimeFlow = localCarTimeFlowMapper.getByTimeKey(Integer.parseInt(dateStr), req.getParkingId());
            dateList.add(DateUtil.convertDate2Str(date1, "MM.dd"));
            if (carTimeFlow != null) {
                expireCountList.add(carTimeFlow.getExpireCount());
                delayCountList.add(carTimeFlow.getDelayCount());
            } else {
                expireCountList.add(0);
                delayCountList.add(0);
            }
        }

        rsp.setDateList(dateList);
        rsp.setExpireCountList(expireCountList);
        rsp.setDelayCountList(delayCountList);
        rsp.setResult(WechatBasicRsp.SUCCESS);
        return rsp;
    }
}
